package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.AppLoginLog;
import com.ruoyi.system.domain.vo.AppLogSearchVo;
import com.ruoyi.system.dto.AppLogSearchDTO;

import com.ruoyi.system.mapper.AppLoginLogMapper;
import com.ruoyi.system.service.IAppLoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppLoginLogServiceImpl extends ServiceImpl<AppLoginLogMapper, AppLoginLog> implements IAppLoginLogService {
    @Override
    public List<AppLogSearchDTO> getList(AppLogSearchVo searchVo) {
        LambdaQueryWrapper<AppLoginLog> wrapper = new LambdaQueryWrapper<>();
        // 关键字模糊搜索
        wrapper.like(StringUtils.isNotBlank(searchVo.getKeyword()),
                AppLoginLog::getUserName, searchVo.getKeyword())
            .or(StringUtils.isNotBlank(searchVo.getKeyword()),
                w -> w.like(AppLoginLog::getMobile, searchVo.getKeyword()));
        // 时间区间
        if (searchVo.getStartTime() != null){
            wrapper.between(AppLoginLog::getLoginTime,searchVo.getStartTime(), searchVo.getEndTime());
        }
        wrapper.orderByDesc(AppLoginLog::getLoginTime);
        List<AppLoginLog> appLoginLogs = baseMapper.selectList(wrapper);
        return appLoginLogs.stream()
            .map(log1 -> {
                AppLogSearchDTO dto = new AppLogSearchDTO();
                BeanUtils.copyProperties(log1, dto);
                return dto;
            })
            .collect(Collectors.toList());
    }

    @Override
    public int countList(AppLogSearchVo searchVo) {
        LambdaQueryWrapper<AppLoginLog> wrapper = new LambdaQueryWrapper<>();
        // 关键字模糊搜索
        wrapper.like(StringUtils.isNotBlank(searchVo.getKeyword()),
                AppLoginLog::getUserName, searchVo.getKeyword())
            .or(StringUtils.isNotBlank(searchVo.getKeyword()),
                w -> w.like(AppLoginLog::getMobile, searchVo.getKeyword()));
        // 时间区间
        if (searchVo.getStartTime() != null){
            wrapper.between(AppLoginLog::getLoginTime,searchVo.getStartTime(), searchVo.getEndTime());
        }
        wrapper.orderByDesc(AppLoginLog::getLoginTime);
        return Math.toIntExact(baseMapper.selectCount(wrapper));
    }

    @Override
    public List<AppLogSearchDTO> getListByPage(AppLogSearchVo searchVo, int pageNo, int pageSize) {
        LambdaQueryWrapper<AppLoginLog> wrapper = new LambdaQueryWrapper<>();
        // 关键字模糊搜索
        wrapper.like(StringUtils.isNotBlank(searchVo.getKeyword()),
                AppLoginLog::getUserName, searchVo.getKeyword())
            .or(StringUtils.isNotBlank(searchVo.getKeyword()),
                w -> w.like(AppLoginLog::getMobile, searchVo.getKeyword()));
        // 时间区间
        if (searchVo.getStartTime() != null){
            wrapper.between(AppLoginLog::getLoginTime,searchVo.getStartTime(), searchVo.getEndTime());
        }
        wrapper.orderByDesc(AppLoginLog::getLoginTime);

        Page<AppLoginLog> page = new Page<>(pageNo, pageSize, false);
        List<AppLoginLog> records = baseMapper.selectPage(page, wrapper).getRecords();
        return records.stream()
            .map(log -> {
                AppLogSearchDTO dto = new AppLogSearchDTO();
                BeanUtils.copyProperties(log, dto);
                return dto;
            })
            .collect(Collectors.toList());
    }
}
